#include "StdAfx.h"
#include "Utility.h"
#include "CustomStatic.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomStatic, CStatic)

CCustomStatic::CCustomStatic(void)
{
	//String and Font Parameter
	m_strText=_T("");
	m_nFontSize = 12;
	m_nFontStyle = FontStyleRegular;
	m_enFontUnit = UnitPoint;
	m_strTextFormat = NULL;
	m_pFontFamily = NULL;
	m_pFontBrush = NULL;
	m_pFont = NULL;

	//Handle Parameter
	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;
}

CCustomStatic::~CCustomStatic(void)
{
	if(m_pFont != NULL){ delete m_pFont;  m_pFont = NULL; }
	if(m_pFontBrush != NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	if(m_pFontFamily != NULL){ delete m_pFontFamily;  m_pFontFamily = NULL; }
	if(m_strTextFormat != NULL){ delete m_strTextFormat;  m_strTextFormat = NULL; }	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomStatic::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//String Font Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomStatic::SetWindowTextW(LPCTSTR lpszString)
{
	CStatic::SetWindowTextW(lpszString);
	Invalidate();
}

void CCustomStatic::SetFontSize(int nSize)
{
	if(nSize > 0)
		m_nFontSize = nSize;
}

void CCustomStatic::SetFontStyle(UINT nStyle)
{
	if(nStyle >= 0)
		m_nFontStyle = nStyle;
}

void CCustomStatic::SetFontColor(int nR, int nG, int nB)
{
	if(m_pFontBrush!=NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	m_pFontBrush = new SolidBrush(Color(247, nR, nG, nB));	

}
void CCustomStatic::SetFontFamily(CString strFontFamilyName)
{
	if(!strFontFamilyName.IsEmpty())
	{
		if(m_pFontFamily!=NULL){ delete m_pFontFamily;  m_pFontFamily=NULL; }
		m_pFontFamily = new FontFamily(strFontFamilyName);
	}
}

void CCustomStatic::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new FontFamily(L"Calibri");
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new Font
	if(m_pFont == NULL)
		m_pFont = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSize, m_nFontStyle, m_enFontUnit);

	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentNear);
		m_strTextFormat->SetLineAlignment(StringAlignmentNear);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}

	//----------------------new FontBrush
	if (m_pFontBrush == NULL)
		m_pFontBrush = new SolidBrush(Color(247, 0, 0, 0));	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomStatic::DrawUI()
{
	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
	Graphics graphics( m_hDrawDC );

	GetWindowTextW(m_strText);
	if(!m_strText.IsEmpty())
	{
		CreateFont();
		graphics.DrawString(m_strText.GetBuffer(), -1, m_pFont, 
			                RectF((Gdiplus::REAL)m_rtRect.left,    (Gdiplus::REAL)m_rtRect.top,
			                      (Gdiplus::REAL)m_rtRect.Width(), (Gdiplus::REAL)m_rtRect.Height()),
			                m_strTextFormat, m_pFontBrush);
	}

	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}

void CCustomStatic::DrawItem(LPDRAWITEMSTRUCT di)
{
	DrawUI();
}

BOOL CCustomStatic::ShowWindow(int nCmdShow)
{
	switch(nCmdShow)
	{
	case SW_SHOW:
		Invalidate();
		break;
	case SW_HIDE:
		BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
		CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
		break;
	default:
		break;
	}

	return CStatic::ShowWindow(nCmdShow);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomStatic, CStatic)
	ON_WM_DRAWITEM()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
void CCustomStatic::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle(0, BS_OWNERDRAW);
	CStatic::PreSubclassWindow();
}

BOOL CCustomStatic::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}